/** 存储/读取/删除 localStorage
 * @param name - 存储名
 * @param content - 存储内容，对象会转为字符串
 **/
export const setLocalStorage = (name: string, content: any) => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
}

export const getLocalStorage = (name: string): any => {
  if (!name) return;
  const content = window.localStorage.getItem(name);

  try {
    return JSON.parse(content);

  } catch(err) {
    return content
  }
}

export const removeLocalStorage = (name: string): void => {
  if (!name) return;
  window.localStorage.removeItem(name);
}

export const setSessionStorage = (name: string, content: any): void => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  window.sessionStorage.setItem(name, content);
}

export const getSessionStorage = (name: string): any => {
  if (!name) return;
  const content = window.sessionStorage.getItem(name);

  try {
    return JSON.parse(content);
  } catch(err) {
    return content
  }
}

export const removeSessionStorage = (name: string): void => {
  if (!name) return;
  window.sessionStorage.removeItem(name);
}
